﻿using UnityEditor;
using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        [CtxMenuItem("Renderer/Mesh/Colors/Clear", "Removes the vertex colors from a mesh(s).", 0)]
        public static void RendererColorsClear()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) continue;
                SkinnContext.UndoRecord(context, "Colors Clear");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                mesh.colors = null;
                mesh.colors32 = null;
                context.SetSharedMesh(mesh);


            }
            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Mesh/Colors/From Material/Color", "Fill vertex colors from a material's color property.", 0)]
        public static void RendererGetColorFromMaterial()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) continue;
                SkinnContext.UndoRecord(context, "Colors From Material");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                var material = context.sharedMaterial; if (!material) continue;
                var colorProp = material.GetColorProperty(); if (string.IsNullOrEmpty(colorProp)) continue;
                var color = material.GetColor(colorProp);
                var colors = new Color32[mesh.vertices.Length];
                for (int i = 0; i < colors.Length; i++) colors[i] = color;
                mesh.colors32 = colors;
                context.SetSharedMesh(mesh);
            }
            SkinnContext.Finished();
        }


        [CtxMenuItem("Renderer/Mesh/Colors/From Material/Color UV Seams", "Fill uv seams from a material's color property.", 0)]
        public static void RendererGetColorFromMaterialUVTest()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) continue;
                SkinnContext.UndoRecord(context, "Colors From Material");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                var material = context.sharedMaterial; if (!material) continue;
                var colorProp = material.GetColorProperty(); if (string.IsNullOrEmpty(colorProp)) continue;
                var color = material.GetColor(colorProp);
                var colors = SkinnEx.IsNullOrEmpty(mesh.colors32, mesh.vertices.Length) ? new Color32[mesh.vertices.Length] : mesh.colors32;
                var seams = mesh.GetSeams();
                for (int i = 0; i < seams.Length; i++) if(seams[i] > 0) colors[i] = color;
                mesh.colors32 = colors;
                context.SetSharedMesh(mesh);
            }

            SkinnContext.Finished();
        }

    }
}